package com.mall.user.services;

import com.alibaba.fastjson.JSON;
import com.google.gson.annotations.JsonAdapter;
import com.mall.user.IUserVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UpdateUserVerifyRequest;
import com.mall.user.dto.UpdateUserVerifyResponse;
import com.mall.user.dto.UserVerifyRequest;
import com.mall.user.dto.UserVerifyResponse;
import com.mall.user.utils.ExceptionProcessorUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @program: cs-mall_35th
 * @description:
 * @author: Weiwei Liu
 * @create: 2021-12-12 12:20
 **/
@Slf4j
@Component
@Service
public class UserVerifyServiceImpl implements IUserVerifyService {


    @Autowired
    UserVerifyMapper userVerifyMapper;

    /**
     * 向验证表中插入验证信息
     *
     * @param request
     * @return
     */
    @Override
    public UserVerifyResponse insertUserVerify(UserVerifyRequest request) {

        UserVerifyResponse response = new UserVerifyResponse();

        String userName = request.getUserName();
        String uuid = request.getUuid();

        try {
            request.requestCheck();
            UserVerify userVerify = new UserVerify();
            userVerify.setUsername(userName);
            userVerify.setUuid(uuid);
            userVerify.setRegisterDate(new Date());
            userVerify.setIsExpire("N");
            userVerify.setIsVerify("N");
            int affectedRows = userVerifyMapper.insert(userVerify);
            if (affectedRows == 0) {
                response.setCode(SysRetCodeConstants.USER_REGISTER_VERIFY_FAILED.getCode());
                response.setCode(SysRetCodeConstants.USER_REGISTER_VERIFY_FAILED.getMessage());
                return response;
            }
        } catch (Exception e) {
            log.error("MemberServiceImpl.uniqueKeyMember Occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
            return response;
        }

        log.info("向验证表中插入验证信息:request,{}", JSON.toJSONString(request));
        // 成功
        response.setCode(SysRetCodeConstants.SUCCESS.getCode());
        response.setCode(SysRetCodeConstants.SUCCESS.getMessage());
        return response;


    }

    /**
     * 更新验证表信息
     *
     * @param request
     * @return
     */
    @Override
    public UpdateUserVerifyResponse updateUserVerify(UpdateUserVerifyRequest request) {

        UpdateUserVerifyResponse response = new UpdateUserVerifyResponse();

        Long id = request.getId();
        String isExpire = request.getIsExpire();
        String isVerify = request.getIsVerify();
        String uuid = request.getUuid();

        try {
            request.requestCheck();

            UserVerify userVerify = new UserVerify();
            userVerify.setId(id);
            userVerify.setIsVerify(isVerify);
            userVerify.setIsExpire(isExpire);
            userVerify.setUuid(uuid);

            int affectedRows = userVerifyMapper.updateByUuid(userVerify);

            if (affectedRows == 0) {
                // 更新验证表信息失败
                response.setCode(SysRetCodeConstants.USER_REGISTER_VERIFY_UPDATE_FAILED.getCode());
                response.setMsg(SysRetCodeConstants.USER_REGISTER_VERIFY_UPDATE_FAILED.getMessage());
                return response;
            }

        } catch (Exception e) {
            log.error("MemberServiceImpl.uniqueKeyMember Occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
            // 验证信息不合法
            response.setCode(SysRetCodeConstants.USER_REGISTER_VERIFY_UPDATE_FAILED.getCode());
            response.setMsg(SysRetCodeConstants.USER_REGISTER_VERIFY_UPDATE_FAILED.getMessage());
            return response;
        }

        log.info("更新验证表信息:request,{}", JSON.toJSONString(request));
        // 成功
        response.setCode(SysRetCodeConstants.SUCCESS.getCode());
        response.setMsg(SysRetCodeConstants.SUCCESS.getMessage());
        return response;


    }
}
